<?php
class Plugin extends MY_Controller {
	protected $_controller_path = '/plugin/Plugin';
	protected $_list_path = '/plugin/list';
	protected $_model = 'plugin/Plugin_model';
	protected $_view_path = 'plugin/view';
	protected $_create_path = 'plugin/create';
	protected $_edit_path = 'plugin/edit';
	protected $_resource_id = self::PLUGIN_RESOURCE;

	function create() {
		$this -> setFormValidation();
		if ($this -> input -> post('action') != 'save') {
			$this -> load -> view('plugin/create');
		} else {
			$filename = $_FILES["plugin_path"]["name"];
			$source = $_FILES["plugin_path"]["tmp_name"];
			$type = $_FILES["plugin_path"]["type"];
			
			$name = explode(".", $filename);
			$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
			foreach($accepted_types as $mime_type) {
				if($mime_type == $type) {
					$okay = true;
					break;
				} 
			}
			$continue = strtolower($name[1]) == 'zip' ? true : false;
			if(!$continue) {
				$message = "The file you are trying to upload is not a .zip file. Please try again.";
			}
			
			$target_path = "plugins/";
			if(move_uploaded_file($source, $target_path . $filename)) {
				$zip = new ZipArchive();
				$x = $zip->open($target_path . $filename);
				if ($x == true) {
					// $zip->extractTo(realpath(APPPATH . "../plugins")); // change this to the correct site path
					$zip->extractTo(APPPATH . "modules"); // change this to the correct site path
					$zip->close();
				} else {
					echo "cannot unzip";
				}
			} else {	
				echo "There was a problem with the upload. Please try again.";
			}
			//read information of plugin
			$plugin_folder = str_replace('.zip', '', $filename);
			$this->readPluginInformation(APPPATH . "modules/".$plugin_folder."/plugin.xml");
			$this->create_table(APPPATH . "modules/".$plugin_folder."/".$plugin_folder.".sql");
		}
	}

	protected function setFormValidation() {
		$this -> form_validation -> set_rules('plugin_path', 'Plugin', 'required');
	}

	protected function init_input_data() {
	echo 'init_input_data';
	}
	
	private function readPluginInformation($xml_path) {
			$this->load->helper('file');
			$plugin_name='';
			$plugin_version='';
			$plugin_description='';
			$plugin_author='';
			$plugin_email='';
			$file_information;
			// get_file_info($xml_path, $file_information);
			// echo read_file($xml_path);
			$doc = new DOMDocument();
			$doc->load($xml_path);
			$names = $doc->getElementsByTagName( "name" );
			$versions = $doc->getElementsByTagName( "version" );
			$descriptions = $doc->getElementsByTagName( "description" );
			$authors = $doc->getElementsByTagName( "author" );
			$emails = $doc->getElementsByTagName( "email" );
			foreach ($names as $name) {
				$plugin_name = $name->nodeValue;
				break;
			}
			foreach ($versions as $version) {
				$plugin_version = $version->nodeValue;
				break;
			}
			foreach ($descriptions as $description) {
				$plugin_description = $description->nodeValue;
				break;
			}
			foreach ($authors as $author) {
				$plugin_author = $author->nodeValue;
				break;
			}
			foreach ($emails as $email) {
				$plugin_email = $email->nodeValue;
				break;
			}
			$data = array(
					'plugin_name' => $plugin_name,
					'version' => $plugin_version,
					'description' => $plugin_description,
					'author' => $plugin_author,
					'email' => $plugin_email,
			);
			$this -> model -> insert($data);
	}
	
	private function create_table($script_path) {
		$sql = file_get_contents($script_path);
		$this->db->query($sql);
	}
	
	public static function register_resource() {
		$resource = array(
			'id' => 3,
			'name' => 'Plugin',
			'description' =>''
		);
	}
}
?>